package com.jlastu.dlxy_androidapp.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.Photo;

import java.util.List;

public class SelectGoodsListAdapter extends RecyclerView.Adapter<SelectGoodsListAdapter.ViewHolder>{
    private List<Goods> mList;

    private List<Photo> mList_P;

    public SelectGoodsListAdapter(List<Goods> list , List<Photo> list_P){
        mList = list;
        mList_P = list_P;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<Goods> list, List<Photo> list_P) {
        mList.clear();
        mList = list;
        mList_P=list_P;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SelectGoodsListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item_select, null);
        SelectGoodsListAdapter.ViewHolder viewHolder = new SelectGoodsListAdapter.ViewHolder(view);
        return viewHolder;
    }

    @SuppressLint("DefaultLocale")
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Goods goods = mList.get(position);
        holder.textView1.setText(goods.getName());
        holder.textView2.setText(goods.getClassify());
        holder.textView3.setText(String.format("%.2f/%s", goods.getPrice(),goods.getUnit()));
        if ( position >= mList.size() || position >= mList_P.size()) {
            // 显示错误图标
            holder.photo.setImageResource(R.drawable.attention);
            return;
        }


        Photo photo = mList_P.get(position);
        holder.photo.setImageBitmap(photo.getBitmap());

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView photo;
        TextView textView1;
        TextView textView2;
        TextView textView3;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            photo = itemView.findViewById(R.id.goods_photo);
            textView1 = itemView.findViewById(R.id.goods_name);
            textView2 = itemView.findViewById(R.id.goods_classify);
            textView3 = itemView.findViewById(R.id.goods_price);
        }
    }
}
